home *** CD-ROM | disk | FTP | other *** search
- /**
- ** $VER: BlkFormat 1.0 (13-Nov-94) Hanns Holger Rutz
- **
- ** formatiert Textdatei/Clipboardinhalt
- ** (linksbündig, rechtsbündig, zentriert, Blocksatz)
- **
- ** Freeware, benötigt OS2.04+.
- ** Geschrieben mit MaxonC++ 1.1, Tabgröße: 3
- **/
-
- #include <exec/types.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <clib/dos_protos.h>
- #include <clib/exec_protos.h>
- #include <clib/iffparse_protos.h>
- #include <exec/memory.h>
-
- #define AllocText( tp, s ) { ULONG sz = (s); struct Text **tptr = (tp);\
- if( *tptr = AllocVec( sz + sizeof( **tptr ), MEMF_PUBLIC )) {\
- (*tptr)->Length = sz;\
- (*tptr)->Data = ((UBYTE *) *tptr) + sizeof( **tptr ); }}
- #define FreeText( t ) FreeVec( (APTR) (t) );
- #define AllocLine( lp, p, c ) { struct NewText **lptr = (lp);\
- if( *lptr = AllocVec( (c) + sizeof( **lptr ), MEMF_PUBLIC )) {\
- (p)->NextLine = (*lptr);\
- (*lptr)->NextLine = NULL;\
- (*lptr)->Columns = 0;\
- (*lptr)->Data = ((UBYTE *) *lptr) + sizeof( **lptr ); }}
- #define FreeLine( l ) FreeVec( (APTR) (l) );
-
- #define ID_FTXT MAKE_ID( 'F', 'T', 'X', 'T' )
- #define ID_CHRS MAKE_ID( 'C', 'H', 'R', 'S' )
-
- struct Text *ReadFile( STRPTR );
- struct Text *ReadClip( LONG );
- struct NewText *ProcessText( struct Text * );
- BOOL WriteFile( struct NewText *, STRPTR );
- BOOL WriteClip( struct NewText *, LONG );
-
- struct Text {
- ULONG Length;
- UBYTE *Data;
- };
-
- struct NewText {
- struct NewText *NextLine;
- UWORD Columns;
- UBYTE *Data;
- };
-
- UBYTE Version[] = "$VER: BlkFormat 1.0 (" __DATE__ ") Hanns Holger Rutz";
- UBYTE Template[] = "FROM,TO,CLIP=CLIPBOARD/K/N,LEFT/S,RIGHT/S,CENTER/S,BLOCK/S,NOMARGIN/S,WIDTH/K/N";
-
- UBYTE *IFFErr[] = {
- "Reached logical end of file",
- "About to leave context",
- "No valid scope for property",
- "Internal memory alloc failed",
- "Stream read error",
- "Stream write error",
- "Stream seek error",
- "Data in file is corrupt",
- "IFF syntax error",
- "Not an IFF file",
- "No call-back hook provided",
- "Client handler normal return",
- "Unknown cause"
- };
- #define NUMERRS (12)
-
- struct Library *IFFParseBase = NULL;
- LONG DefaultWidth = 79;
-
- struct {
- STRPTR From;
- STRPTR To;
- LONG *Clip;
- ULONG Left;
- ULONG Right;
- ULONG Center;
- ULONG Block;
- ULONG NoMargin;
- LONG *Width;
- } ArgArray = { NULL, NULL, NULL, 0UL, 0UL, 0UL, 0UL, 0UL, &DefaultWidth };
-
- int main( void )
- {
- int Res = RETURN_FAIL;
- struct RDArgs *RDArgs;
-
- if( RDArgs = ReadArgs( Template, (LONG *) &ArgArray, NULL ))
- {
- if( (ArgArray.From) || (ArgArray.Clip) )
- {
- struct Text *Tx;
-
- if( (!ArgArray.Left) && (!ArgArray.Right) && (!ArgArray.Center) )
- ArgArray.Block = TRUE;
-
- if( (ArgArray.From) ) Tx = ReadFile( ArgArray.From );
- else Tx = ReadClip( *(ArgArray.Clip) );
-
- if( Tx )
- {
- BOOL Succ;
- struct NewText *Ln;
-
- if( Ln = ProcessText( Tx ))
- {
- if( (ArgArray.To) ) Succ = WriteFile( Ln, ArgArray.To );
- else {
- if( (ArgArray.Clip) ) Succ = WriteClip( Ln, *(ArgArray.Clip) );
- else Succ = WriteFile( Ln, ArgArray.From );
- }
- if( Succ ) Res = RETURN_OK;
-
- FreeText( Tx );
- }
- }
- }
- else PrintFault( ERROR_REQUIRED_ARG_MISSING, NULL );
-
- FreeArgs( RDArgs );
- }
- else PrintFault( IoErr(), NULL );
-
- CloseLibrary( IFFParseBase );
-
- exit( Res );
- }
-
- VOID LineLength( struct Text *Tx, ULONG *TxOffs, UWORD *Len, UWORD *RealLen,
- UWORD *Spaces, UWORD *LeadSpaces )
- {
- UBYTE LastChar = 0;
- ULONG Start = *TxOffs;
-
- *Spaces = 0;
- *LeadSpaces = 0;
- *Len = 0;
-
- for( ; *TxOffs < Tx->Length && LastChar != '\n'; (*TxOffs)++ )
- {
- switch( Tx->Data[ *TxOffs ] )
- {
- case ' ' : if( !(*Len) )
- {
- (*LeadSpaces)++;
- }
- else if( LastChar != ' ' )
- {
- (*Spaces)++;
- (*Len)++;
- }
- LastChar = ' ';
- break;
-
- case 0x9B:
- case 0x1B: while( *TxOffs + 1 < Tx->Length
- && Tx->Data[ *TxOffs + 1 ] != '\n'
- && Tx->Data[ *TxOffs ] != 'm' )
- (*TxOffs)++;
- break;
-
- default: (*Len)++;
- case '\n': LastChar = Tx->Data[ *TxOffs ];
- break;
- }
- }
- *RealLen = (UWORD) (*TxOffs - Start);
- }
-
- VOID CopyLine( struct NewText *Ln, struct Text *Tx, ULONG Offs, UWORD LeadSpaces,
- float SpaceWidth )
- {
- UBYTE LastChar = 0;
- UBYTE *Line = &(Ln->Data[ 0 ]);
- BOOL Leading = TRUE;
- float Remember = 0;
- int i;
-
- for( ; (LeadSpaces); LeadSpaces-- )
- *(Line++) = ' ';
-
- for( ; Offs < Tx->Length && LastChar != '\n'; Offs++ )
- {
- switch( Tx->Data[ Offs ] )
- {
- case ' ' : if( LastChar != ' ' && (!Leading) )
- {
- if( !SpaceWidth )
- {
- *(Line++) = ' ';
- }
- else
- {
- i = (int) (SpaceWidth + Remember + 1.5);
- Remember = SpaceWidth + Remember + 1 - i;
- for( ; i > 0; i-- )
- *(Line++) = ' ';
- }
- LastChar = ' ';
- }
- break;
-
- case 0x9B:
- case 0x1B: do
- {
- *(Line++) = Tx->Data[ Offs ];
- Offs++;
- } while( Offs < Tx->Length && Tx->Data[ Offs ] != '\n'
- && Tx->Data[ Offs - 1 ] != 'm' );
- Offs--;
- break;
-
- default: LastChar = Tx->Data[ Offs ];
- *(Line++) = LastChar;
- Leading = FALSE;
- break;
- }
- }
- Ln->Columns = (UWORD) (Line - Ln->Data);
- }
-
- VOID FreeNewText( struct NewText *Ln )
- {
- struct NewText *Prev;
-
- while( (Ln) )
- {
- Prev = Ln;
- Ln = Ln->NextLine;
- FreeLine( Prev );
- }
- }
-
- struct NewText *ProcessText( struct Text *Tx )
- {
- UWORD Len,
- RealLen,
- Spaces,
- LeadSpaces;
- WORD Diff;
- ULONG TxOffs = 0UL;
- struct NewText *Line = NULL,
- *Prev = NULL,
- *FirstLine = NULL;
-
- while( TxOffs < Tx->Length )
- {
- LineLength( Tx, &TxOffs, &Len, &RealLen, &Spaces, &LeadSpaces );
- if( ArgArray.NoMargin ) LeadSpaces = 0;
-
- Prev = Line;
- AllocLine( &Line, Prev, RealLen + *(ArgArray.Width) );
- if( !Line )
- {
- FreeNewText( FirstLine );
- return( NULL );
- }
- if( !Prev ) FirstLine = Line;
-
- if( ArgArray.Block ) // BLOCK
- {
- Diff = *(ArgArray.Width) - Len - LeadSpaces;
- if( (Diff <= 0) || (!Spaces) )
- CopyLine( Line, Tx, TxOffs - RealLen, LeadSpaces, 0 );
- else
- CopyLine( Line, Tx, TxOffs - RealLen, LeadSpaces,
- ((float) Diff) / ((float) Spaces) );
- }
- else
- {
- if( ArgArray.Center ) // CENTER
- {
- if( Len + 1 >= *(ArgArray.Width) )
- CopyLine( Line, Tx, TxOffs - RealLen, 0, 0 );
- else
- CopyLine( Line, Tx, TxOffs - RealLen,
- (*(ArgArray.Width) - Len) >> 1, 0 );
- }
- else
- {
- if( ArgArray.Right ) // RIGHT
- {
- if( Len >= *(ArgArray.Width) )
- CopyLine( Line, Tx, TxOffs - RealLen, 0, 0 );
- else
- CopyLine( Line, Tx, TxOffs - RealLen, *(ArgArray.Width) - Len, 0 );
- }
- else
- { // LEFT
- CopyLine( Line, Tx, TxOffs - RealLen, LeadSpaces, 0 );
- }
- }
- }
- }
- return( FirstLine );
- }
-
- struct Text *ReadFile( STRPTR Name )
- {
- struct Text *Tx = NULL;
- struct FileInfoBlock *fib;
-
- if( fib = AllocDosObject( DOS_FIB, NULL ))
- {
- BPTR fh;
-
- if( fh = Open( Name, MODE_OLDFILE ))
- {
- if( ExamineFH( fh, fib ))
- {
- AllocText( &Tx, fib->fib_Size );
-
- if( Tx )
- {
- if( Read( fh, Tx->Data, Tx->Length ) == -1 )
- {
- PrintFault( IoErr(), Name );
- FreeText( Tx );
- Tx = NULL;
- }
- }
- else PrintFault( ERROR_NO_FREE_STORE, NULL );
- }
- else PrintFault( IoErr(), Name );
-
- Close( fh );
- }
- else PrintFault( IoErr(), Name );
-
- FreeDosObject( DOS_FIB, fib );
- }
- else PrintFault( ERROR_NO_FREE_STORE, NULL );
-
- return( Tx );
- }
-
- BOOL WriteFile( struct NewText *Ln, STRPTR Name )
- {
- BPTR fh;
- BOOL Succ = FALSE;
-
- if( fh = Open( Name, MODE_NEWFILE ))
- {
- for( ; (Ln); Ln = Ln->NextLine )
- {
- if( Write( fh, Ln->Data, Ln->Columns ) < 0 ) break;
- }
- Succ = (Ln == NULL);
- if( (!Close( fh )) || (!Succ) )
- {
- Succ = FALSE;
- PrintFault( IoErr(), Name );
- }
- }
- else PrintFault( IoErr(), Name );
-
- return( Succ );
- }
-
- struct Text *ReadClip( LONG Unit )
- {
- struct IFFHandle *iff;
- struct Text *Tx = NULL;
-
- if( IFFParseBase == NULL )
- {
- if( !(IFFParseBase = OpenLibrary( "iffparse.library", 36 )))
- {
- PutStr( "Couldn't open `iffparse.library' v36+!\n" );
- return( FALSE );
- }
- }
-
- if( iff = AllocIFF())
- {
- InitIFFasClip( iff );
-
- if( iff->iff_Stream = (ULONG) OpenClipboard( *ArgArray.Clip ))
- {
- LONG Err;
-
- if( !(Err = OpenIFF( iff, IFFF_READ )))
- {
- if( !(Err = StopChunk( iff, ID_FTXT, ID_CHRS )))
- {
- while( (Err = ParseIFF( iff, IFFPARSE_SCAN )) == IFFERR_EOC );
-
- if( (!Err) || Err == IFFERR_EOF )
- {
- struct ContextNode *cn = CurrentChunk( iff );
-
- if( (cn) && cn->cn_Type == ID_FTXT && cn->cn_ID == ID_CHRS )
- {
- AllocText( &Tx, cn->cn_Size );
-
- if( Tx )
- {
- if( ReadChunkBytes( iff, Tx->Data, Tx->Length ) != Tx->Length )
- {
- Err = IFFERR_READ;
- FreeText( Tx );
- Tx = NULL;
- }
- }
- else PrintFault( ERROR_NO_FREE_STORE, NULL );
- }
- else VPrintf( "No text in clipboard unit %ld!\n", ArgArray.Clip );
- }
- }
- }
- CloseIFF( iff );
- CloseClipboard( (struct ClipboardHandle *) iff->iff_Stream );
-
- if( (Err) && Err != IFFERR_EOF )
- {
- if( -Err < 1 || -Err > NUMERRS ) Err = -NUMERRS - 1;
- VPrintf( "Clipboard error: %s\n", (LONG *) &IFFErr[ -Err - 1 ] );
- }
- }
- else VPrintf( "Couldn't open clipboard unit %ld!\n", ArgArray.Clip );
-
- FreeIFF( iff );
- }
- else PrintFault( ERROR_NO_FREE_STORE, NULL );
-
- return( Tx );
- }
-
- BOOL WriteClip( struct NewText *Ln, LONG Unit )
- {
- struct IFFHandle *iff;
- BOOL Succ = FALSE;
-
- if( IFFParseBase == NULL )
- {
- if( !(IFFParseBase = OpenLibrary( "iffparse.library", 36 )))
- {
- PutStr( "Couldn't open `iffparse.library' v36+!\n" );
- return( FALSE );
- }
- }
-
- if( iff = AllocIFF())
- {
- InitIFFasClip( iff );
-
- if( iff->iff_Stream = (ULONG) OpenClipboard( *ArgArray.Clip ))
- {
- LONG Err;
-
- if( !(Err = OpenIFF( iff, IFFF_WRITE )))
- {
- if( !(Err = PushChunk( iff, ID_FTXT, ID_FORM, IFFSIZE_UNKNOWN )))
- {
- if( !(Err = PushChunk( iff, 0, ID_CHRS, IFFSIZE_UNKNOWN )))
- {
- for( ; (Ln); Ln = Ln->NextLine )
- {
- if( WriteChunkBytes( iff, Ln->Data, Ln->Columns ) != Ln->Columns )
- break;
- }
- if( !Ln )
- {
- if( !(Err = PopChunk( iff )))
- Err = PopChunk( iff );
- }
- else Err = IFFERR_WRITE;
- }
- }
- }
- CloseIFF( iff );
- CloseClipboard( (struct ClipboardHandle *) iff->iff_Stream );
-
- if( Err )
- {
- if( -Err < 1 || -Err > NUMERRS ) Err = -NUMERRS - 1;
- VPrintf( "Clipboard error: %s\n", (LONG *) &IFFErr[ -Err - 1 ] );
- }
- else Succ = TRUE;
- }
- else VPrintf( "Couldn't open clipboard unit %ld!\n", ArgArray.Clip );
-
- FreeIFF( iff );
- }
- else PrintFault( ERROR_NO_FREE_STORE, NULL );
-
- return( Succ );
- }
-